home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / info-service / wais / ir-book-sources / stemmer / stem.c < prev    next >
C/C++ Source or Header  |  1993-04-08  |  18KB  |  487 lines

  1.  
  2. /*******************************   stem.c   ***********************************
  3.  
  4.    Purpose:    Implementation of the Porter stemming algorithm documented 
  5.                in: Porter, M.F., "An Algorithm For Suffix Stripping," 
  6.                Program 14 (3), July 1980, pp. 130-137.
  7.  
  8.    Provenance: Written by B. Frakes and C. Cox, 1986.
  9.                Changed by C. Fox, 1990.
  10.                   - made measure function a DFA
  11.                   - restructured structs
  12.                   - renamed functions and variables
  13.                   - restricted function and variable scopes
  14.                Changed by C. Fox, July, 1991.
  15.                   - added ANSI C declarations 
  16.                   - branch tested to 90% coverage
  17.  
  18.    Notes:      This code will make little sense without the the Porter
  19.                article.  The stemming function converts its input to
  20.                lower case.
  21. **/
  22.  
  23. /************************   Standard Include Files   *************************/
  24.  
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <ctype.h>
  28.  
  29. /*****************************************************************************/
  30. /*****************   Private Defines and Data Structures   *******************/
  31.  
  32. #define FALSE                         0
  33. #define TRUE                          1
  34. #define EOS                         '\0'
  35.  
  36. #define IsVowel(c)        ('a'==(c)||'e'==(c)||'i'==(c)||'o'==(c)||'u'==(c))
  37.  
  38. typedef struct {
  39.            int id;                 /* returned if rule fired */
  40.            char *old_end;          /* suffix replaced */
  41.            char *new_end;          /* suffix replacement */
  42.            int old_offset;         /* from end of word to start of suffix */
  43.            int new_offset;         /* from beginning to end of new suffix */
  44.            int min_root_size;      /* min root word size for replacement */
  45.            int (*condition)();     /* the replacement test function */
  46.            } RuleList;
  47.  
  48. static char LAMBDA[1] = "";        /* the constant empty string */
  49. static char *end;                  /* pointer to the end of the word */
  50.  
  51. /*****************************************************************************/
  52. /********************   Private Function Declarations   **********************/
  53.  
  54. #ifdef __STDC__
  55.  
  56. static int WordSize( char *word );
  57. static int ContainsVowel( char *word );
  58. static int EndsWithCVC( char *word );
  59. static int AddAnE( char *word );
  60. static int RemoveAnE( char *word );
  61. static int ReplaceEnd( char *word, RuleList rule );
  62.  
  63. #else
  64.  
  65. static int WordSize( /* word */ );
  66. static int ContainsVowel( /* word */ );
  67. static int EndsWithCVC( /* word */ );
  68. static int AddAnE( /* word */ );
  69. static int RemoveAnE( /* word */ );
  70. static int ReplaceEnd( /* word, rule */ );
  71.  
  72. #endif
  73.  
  74. /******************************************************************************/
  75. /*****************   Initialized Private Data Structures   ********************/
  76.  
  77. static RuleList step1a_rules[] =
  78.            {
  79.              101,  "sses",      "ss",    3,  1, -1,  NULL,
  80.              102,  "ies",       "i",     2,  0, -1,  NULL,
  81.              103,  "ss",        "ss",    1,  1, -1,  NULL,
  82.              104,  "s",         LAMBDA,  0, -1, -1,  NULL,
  83.              000,  NULL,        NULL,    0,  0,  0,  NULL,
  84.            };
  85.  
  86. static RuleList step1b_rules[] =
  87.            {
  88.              105,  "eed",       "ee",    2,  1,  0,  NULL,
  89.              106,  "ed",        LAMBDA,  1, -1, -1,  ContainsVowel,
  90.              107,  "ing",       LAMBDA,  2, -1, -1,  ContainsVowel,
  91.              000,  NULL,        NULL,    0,  0,  0,  NULL,
  92.            };
  93.  
  94. static RuleList step1b1_rules[] =
  95.            {
  96.              108,  "at",        "ate",   1,  2, -1,  NULL,
  97.              109,  "bl",        "ble",   1,  2, -1,  NULL,
  98.              110,  "iz",        "ize",   1,  2, -1,  NULL,
  99.              111,  "bb",        "b",     1,  0, -1,  NULL,
  100.              112,  "dd",        "d",     1,  0, -1,  NULL,
  101.              113,  "ff",        "f",     1,  0, -1,  NULL,
  102.              114,  "gg",        "g",     1,  0, -1,  NULL,
  103.              115,  "mm",        "m",     1,  0, -1,  NULL,
  104.              116,  "nn",        "n",     1,  0, -1,  NULL,
  105.              117,  "pp",        "p",     1,  0, -1,  NULL,
  106.              118,  "rr",        "r",     1,  0, -1,  NULL,
  107.              119,  "tt",        "t",     1,  0, -1,  NULL,
  108.              120,  "ww",        "w",     1,  0, -1,  NULL,
  109.              121,  "xx",        "x",     1,  0, -1,  NULL,
  110.              122,  LAMBDA,      "e",    -1,  0, -1,  AddAnE,
  111.              000,  NULL,        NULL,    0,  0,  0,  NULL,
  112.              };
  113.  
  114. static RuleList step1c_rules[] =
  115.            {
  116.              123,  "y",         "i",      0,  0, -1,  ContainsVowel,
  117.              000,  NULL,        NULL,    0,  0,  0,  NULL,
  118.            };
  119.  
  120. static RuleList step2_rules[] =
  121.            {
  122.              203,  "ational",   "ate",   6,  2,  0,  NULL,
  123.              204,  "tional",    "tion",  5,  3,  0,  NULL,
  124.              205,  "enci",      "ence",  3,  3,  0,  NULL,
  125.              206,  "anci",      "ance",  3,  3,  0,  NULL,
  126.              207,  "izer",      "ize",   3,  2,  0,  NULL,
  127.              208,  "abli",      "able",  3,  3,  0,  NULL,
  128.              209,  "alli",      "al",    3,  1,  0,  NULL,
  129.              210,  "entli",     "ent",   4,  2,  0,  NULL,
  130.              211,  "eli",       "e",     2,  0,  0,  NULL,
  131.              213,  "ousli",     "ous",   4,  2,  0,  NULL,
  132.              214,  "ization",   "ize",   6,  2,  0,  NULL,
  133.              215,  "ation",     "ate",   4,  2,  0,  NULL,
  134.              216,  "ator",      "ate",   3,  2,  0,  NULL,
  135.              217,  "alism",     "al",    4,  1,  0,  NULL,
  136.              218,  "iveness",   "ive",   6,  2,  0,  NULL,
  137.              219,  "fulnes",    "ful",   5,  2,  0,  NULL,
  138.              220,  "ousness",   "ous",   6,  2,  0,  NULL,
  139.              221,  "aliti",     "al",    4,  1,  0,  NULL,
  140.              222,  "iviti",     "ive",   4,  2,  0,  NULL,
  141.              223,  "biliti",    "ble",   5,  2,  0,  NULL,
  142.              000,  NULL,        NULL,    0,  0,  0,  NULL,
  143.            };
  144.  
  145. static RuleList step3_rules[] =
  146.            {
  147.              301,  "icate",     "ic",    4,  1,  0,  NULL,
  148.              302,  "ative",     LAMBDA,  4, -1,  0,  NULL,
  149.              303,  "alize",     "al",    4,  1,  0,  NULL,
  150.              304,  "iciti",     "ic",    4,  1,  0,  NULL,
  151.              305,  "ical",      "ic",    3,  1,  0,  NULL,
  152.              308,  "ful",       LAMBDA,  2, -1,  0,  NULL,
  153.              309,  "ness",      LAMBDA,  3, -1,  0,  NULL,
  154.              000,  NULL,        NULL,    0,  0,  0,  NULL,
  155.            };
  156.  
  157. static RuleList step4_rules[] =
  158.            {
  159.              401,  "al",        LAMBDA,  1, -1,  1,  NULL,
  160.              402,  "ance",      LAMBDA,  3, -1,  1,  NULL,
  161.              403,  "ence",      LAMBDA,  3, -1,  1,  NULL,
  162.              405,  "er",        LAMBDA,  1, -1,  1,  NULL,
  163.              406,  "ic",        LAMBDA,  1, -1,  1,  NULL,
  164.              407,  "able",      LAMBDA,  3, -1,  1,  NULL,
  165.              408,  "ible",      LAMBDA,  3, -1,  1,  NULL,
  166.              409,  "ant",       LAMBDA,  2, -1,  1,  NULL,
  167.              410,  "ement",     LAMBDA,  4, -1,  1,  NULL,
  168.              411,  "ment",      LAMBDA,  3, -1,  1,  NULL,
  169.              412,  "ent",       LAMBDA,  2, -1,  1,  NULL,
  170.              423,  "sion",      "s",     3,  0,  1,  NULL,
  171.              424,  "tion",      "t",     3,  0,  1,  NULL,
  172.              415,  "ou",        LAMBDA,  1, -1,  1,  NULL,
  173.              416,  "ism",       LAMBDA,  2, -1,  1,  NULL,
  174.              417,  "ate",       LAMBDA,  2, -1,  1,  NULL,
  175.              418,  "iti",       LAMBDA,  2, -1,  1,  NULL,
  176.              419,  "ous",       LAMBDA,  2, -1,  1,  NULL,
  177.              420,  "ive",       LAMBDA,  2, -1,  1,  NULL,
  178.              421,  "ize",       LAMBDA,  2, -1,  1,  NULL,
  179.              000,  NULL,        NULL,    0,  0,  0,  NULL,
  180.            };
  181.  
  182. static RuleList step5a_rules[] =
  183.            {
  184.              501,  "e",         LAMBDA,  0, -1,  1,  NULL,
  185.              502,  "e",         LAMBDA,  0, -1, -1,  RemoveAnE,
  186.              000,  NULL,        NULL,    0,  0,  0,  NULL,
  187.            };
  188.  
  189. static RuleList step5b_rules[] =
  190.            {
  191.              503,  "ll",        "l",     1,  0,  1,  NULL,
  192.              000,  NULL,        NULL,    0,  0,  0,  NULL,
  193.            };
  194.  
  195. /*****************************************************************************/
  196. /********************   Private Function Declarations   **********************/
  197.  
  198. /*FN***************************************************************************
  199.  
  200.        WordSize( word )
  201.  
  202.    Returns: int -- a weird count of word size in adjusted syllables
  203.  
  204.    Purpose: Count syllables in a special way:  count the number 
  205.             vowel-consonant pairs in a word, disregarding initial 
  206.             consonants and final vowels.  The letter "y" counts as a
  207.             consonant at the beginning of a word and when it has a vowel
  208.             in front of it; otherwise (when it follows a consonant) it
  209.             is treated as a vowel.  For example, the WordSize of "cat" 
  210.             is 1, of "any" is 1, of "amount" is 2, of "anything" is 3.
  211.  
  212.    Plan:    Run a DFA to compute the word size
  213.  
  214.    Notes:   The easiest and fastest way to compute this funny measure is
  215.             with a finite state machine.  The initial state 0 checks
  216.             the first letter.  If it is a vowel, then the machine changes
  217.             to state 1, which is the "last letter was a vowel" state.
  218.             If the first letter is a consonant or y, then it changes
  219.             to state 2, the "last letter was a consonant state".  In
  220.             state 1, a y is treated as a consonant (since it follows
  221.             a vowel), but in state 2, y is treated as a vowel (since
  222.             it follows a consonant.  The result counter is incremented
  223.             on the transition from state 1 to state 2, since this
  224.             transition only occurs after a vowel-consonant pair, which
  225.             is what we are counting.
  226. **/
  227.  
  228. static int
  229. WordSize( word )
  230.    char *word;   /* in: word having its WordSize taken */
  231.    {
  232.    register int result;   /* WordSize of the word */
  233.    register int state;    /* current state in machine */
  234.  
  235.    result = 0;
  236.    state = 0;
  237.  
  238.                  /* Run a DFA to compute the word size */
  239.    while ( EOS != *word )
  240.       {
  241.       switch ( state )
  242.          {
  243.          case 0: state = (IsVowel(*word)) ? 1 : 2;
  244.                  break;
  245.          case 1: state = (IsVowel(*word)) ? 1 : 2;
  246.                  if ( 2 == state ) result++;
  247.                  break;
  248.          case 2: state = (IsVowel(*word) || ('y' == *word)) ? 1 : 2;
  249.                  break;
  250.          }
  251.       word++;
  252.       }
  253.  
  254.    return( result );
  255.  
  256.    } /* WordSize */
  257.  
  258. /*FN**************************************************************************
  259.  
  260.        ContainsVowel( word )
  261.  
  262.    Returns: int -- TRUE (1) if the word parameter contains a vowel,
  263.             FALSE (0) otherwise.
  264.  
  265.    Purpose: Some of the rewrite rules apply only to a root containing
  266.             a vowel, where a vowel is one of "aeiou" or y with a
  267.             consonant in front of it.
  268.  
  269.    Plan:    Obviously, under the definition of a vowel, a word contains
  270.             a vowel iff either its first letter is one of "aeiou", or
  271.             any of its other letters are "aeiouy".  The plan is to
  272.             test this condition.
  273.  
  274.    Notes:   None
  275. **/
  276.  
  277. static int
  278. ContainsVowel( word )
  279.    char *word;   /* in: buffer with word checked */
  280.    {
  281.  
  282.    if ( EOS == *word )
  283.       return( FALSE );
  284.    else
  285.       return( IsVowel(*word) || (NULL != strpbrk(word+1,"aeiouy")) );
  286.  
  287.  
  288.    } /* ContainsVowel */
  289.  
  290. /*FN**************************************************************************
  291.  
  292.        EndsWithCVC( word )
  293.  
  294.    Returns: int -- TRUE (1) if the current word ends with a
  295.             consonant-vowel-consonant combination, and the second
  296.             consonant is not w, x, or y, FALSE (0) otherwise.
  297.  
  298.    Purpose: Some of the rewrite rules apply only to a root with
  299.             this characteristic.
  300.  
  301.    Plan:    Look at the last three characters.
  302.  
  303.    Notes:   None
  304. **/
  305.  
  306. static int
  307. EndsWithCVC( word )
  308.    char *word;   /* in: buffer with the word checked */
  309.    {
  310.    int length;         /* for finding the last three characters */
  311.  
  312.    if ( (length = strlen(word)) < 2 )
  313.       return( FALSE );
  314.    else
  315.       {
  316.       end = word + length - 1;
  317.       return(    (NULL == strchr("aeiouwxy",*end--))      /* consonant */
  318.               && (NULL != strchr("aeiouy",  *end--))        /* vowel */
  319.               && (NULL == strchr("aeiou",   *end  )) );   /* consonant */
  320.       }
  321.  
  322.    } /* EndsWithCVC */
  323.  
  324. /*FN**************************************************************************
  325.  
  326.        AddAnE( word )
  327.  
  328.    Returns: int -- TRUE (1) if the current word meets special conditions
  329.             for adding an e.
  330.  
  331.    Purpose: Rule 122 applies only to a root with this characteristic.
  332.  
  333.    Plan:    Check for size of 1 and a consonant-vowel-consonant ending.
  334.  
  335.    Notes:   None
  336. **/
  337.  
  338. static int
  339. AddAnE( word )
  340.    char *word;
  341.    {
  342.  
  343.    return( (1 == WordSize(word)) && EndsWithCVC(word) );
  344.  
  345.    } /* AddAnE */
  346.  
  347. /*FN**************************************************************************
  348.  
  349.        RemoveAnE( word )
  350.  
  351.    Returns: int -- TRUE (1) if the current word meets special conditions
  352.             for removing an e.
  353.  
  354.    Purpose: Rule 502 applies only to a root with this characteristic.
  355.  
  356.    Plan:    Check for size of 1 and no consonant-vowel-consonant ending.
  357.  
  358.    Notes:   None
  359. **/
  360.  
  361. static int
  362. RemoveAnE( word )
  363.    char *word;
  364.    {
  365.  
  366.    return( (1 == WordSize(word)) && !EndsWithCVC(word) );
  367.  
  368.    } /* RemoveAnE */
  369.  
  370. /*FN**************************************************************************
  371.  
  372.        ReplaceEnd( word, rule )
  373.  
  374.    Returns: int -- the id for the rule fired, 0 is none is fired
  375.  
  376.    Purpose: Apply a set of rules to replace the suffix of a word
  377.  
  378.    Plan:    Loop through the rule set until a match meeting all conditions
  379.             is found.  If a rule fires, return its id, otherwise return 0.
  380.             Connditions on the length of the root are checked as part of this
  381.             function's processing because this check is so often made.
  382.  
  383.    Notes:   This is the main routine driving the stemmer.  It goes through
  384.             a set of suffix replacement rules looking for a match on the
  385.             current suffix.  When it finds one, if the root of the word
  386.             is long enough, and it meets whatever other conditions are
  387.             required, then the suffix is replaced, and the function returns.
  388. **/
  389.  
  390. static int
  391. ReplaceEnd( word, rule )
  392.    char *word;        /* in/out: buffer with the stemmed word */
  393.    RuleList *rule;    /* in: data structure with replacement rules */
  394.    {
  395.    register char *ending;   /* set to start of possible stemmed suffix */
  396.    char tmp_ch;             /* save replaced character when testing */
  397.  
  398.    while ( 0 != rule->id )
  399.       {
  400.       ending = end - rule->old_offset;
  401.       if ( word <= ending )
  402.          if ( 0 == strcmp(ending,rule->old_end) )
  403.             {
  404.             tmp_ch = *ending;
  405.             *ending = EOS;
  406.             if ( rule->min_root_size < WordSize(word) )
  407.                if ( !rule->condition || (*rule->condition)(word) )
  408.                   {
  409.                   (void)strcat( word, rule->new_end );
  410.                   end = ending + rule->new_offset;
  411.                   break;
  412.                   }
  413.             *ending = tmp_ch;
  414.             }
  415.       rule++;
  416.       }
  417.  
  418.    return( rule->id );
  419.  
  420.    } /* ReplaceEnd */
  421.  
  422. /*****************************************************************************/
  423. /*********************   Public Function Declarations   **********************/
  424.  
  425. /*FN***************************************************************************
  426.  
  427.        Stem( word )
  428.  
  429.    Returns: int -- FALSE (0) if the word contains non-alphabetic characters
  430.             and hence is not stemmed, TRUE (1) otherwise
  431.  
  432.    Purpose: Stem a word
  433.  
  434.    Plan:    Part 1: Check to ensure the word is all alphabetic
  435.             Part 2: Run through the Porter algorithm
  436.             Part 3: Return an indication of successful stemming
  437.  
  438.    Notes:   This function implements the Porter stemming algorithm, with
  439.             a few additions here and there.  See:
  440.  
  441.                Porter, M.F., "An Algorithm For Suffix Stripping,"
  442.                Program 14 (3), July 1980, pp. 130-137.
  443.  
  444.             Porter's algorithm is an ad hoc set of rewrite rules with
  445.             various conditions on rule firing.  The terminology of
  446.             "step 1a" and so on, is taken directly from Porter's
  447.             article, which unfortunately gives almost no justification
  448.             for the various steps.  Thus this function more or less
  449.             faithfully refects the opaque presentation in the article.
  450.             Changes from the article amount to a few additions to the
  451.             rewrite rules;  these are marked in the RuleList data
  452.             structures with comments.
  453. **/
  454.  
  455. int
  456. Stem( word )
  457.    char *word;  /* in/out: the word stemmed */
  458.    {
  459.    int rule;    /* which rule is fired in replacing an end */
  460.  
  461.             /* Part 1: Check to ensure the word is all alphabetic */
  462.    for ( end = word; *end != EOS; end++ )
  463.       if ( !isalpha(*end) ) return( FALSE );
  464.       else *end = tolower( *end );
  465.    end--;
  466.  
  467.                 /*  Part 2: Run through the Porter algorithm */
  468.    (void)ReplaceEnd( word, step1a_rules );
  469.    rule = ReplaceEnd( word, step1b_rules );
  470.    if ( (106 == rule) || (107 == rule) )
  471.       (void)ReplaceEnd( word, step1b1_rules );
  472.    (void)ReplaceEnd( word, step1c_rules );
  473.  
  474.    (void)ReplaceEnd( word, step2_rules );
  475.  
  476.    (void)ReplaceEnd( word, step3_rules );
  477.  
  478.    (void)ReplaceEnd( word, step4_rules );
  479.  
  480.    (void)ReplaceEnd( word, step5a_rules );
  481.    (void)ReplaceEnd( word, step5b_rules );
  482.  
  483.            /* Part 3: Return an indication of successful stemming */
  484.    return( TRUE );
  485.  
  486.    } /* Stem */
  487.